package org.hhqk.admin.controller;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.hhqk.admin.activemq.ConsumerService;
import org.hhqk.admin.activemq.ProducerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 
 * @description controller测试
 */
@Controller
@RequestMapping("/activemq")
public class ActivemqController {
	
	//队列名 hhqk.admin
    @Resource(name="demoQueueDestination")
    private Destination demoQueueDestination;

    //队列消息生产者
    @Resource(name="producerService")
    private ProducerService producer;
    
    //队列消息消费者
    @Resource(name="consumerService")
    private ConsumerService consumer;
	
	/**
	 * 发送消息到队列
	 * Queue队列：仅有一个订阅者会收到消息，消息一旦被处理就不会存在队列中
	 * @param message
	 * @return String
	 */
	@ResponseBody
	@RequestMapping("queueSender")
	public String queueSender(@RequestParam("message")String message){
		String opt="";
		try {
			producer.sendMessage(demoQueueDestination, message);
			opt = "suc";
		} catch (Exception e) {
			opt = e.getCause().toString();
		}
		return opt;
	}
	
	
	/**
	 * 手动接收消息从队列，
	 * 如果有配置消息监听，可负责自动监听activemq中的队列消息，一旦有新消息，将自动读取消费
	 * @param void
	 * @return String
	 */
	@ResponseBody
	@RequestMapping("queueReceive")
	public String queueReceive(){
		String opt="";
		try {
			consumer.receive(demoQueueDestination);
			opt = "receiv-suc";
		} catch (Exception e) {
			opt = e.getCause().toString();
		}
		return opt;
	}
	
}
